var chess = document.getElementById("chess");
var context = chess.getContext("2d");
context.strokeStyle = "black";

var clickFlag = true;

function loadCanvas() {
    for (var i = 0; i < 15; i++) {
        context.moveTo(15, 15 + 37.1 * i);
        context.lineTo(535, 15 + 37.1 * i);
        context.stroke();

        context.moveTo(15 + 37.1 * i, 15);
        context.lineTo(15 + 37.1 * i, 535);
        context.stroke();
    }

    context.beginPath();
    context.arc(15 + 37.1 * 3, 15 + 37.1 * 3, 4, 0, 2 * Math.PI)
    context.fillStyle = "black";
    context.fill();

    context.beginPath();
    context.arc(15 + 37.1 * 11, 15 + 37.1 * 3, 4, 0, 2 * Math.PI)
    context.fillStyle = "black";
    context.fill();

    context.beginPath();
    context.arc(15 + 37.1 * 11, 15 + 37.1 * 11, 4, 0, 2 * Math.PI)
    context.fillStyle = "black";
    context.fill();

    context.beginPath();
    context.arc(15 + 37.1 * 3, 15 + 37.1 * 11, 4, 0, 2 * Math.PI)
    context.fillStyle = "black";
    context.fill();

    context.beginPath();
    context.arc(15 + 37.1 * 7, 15 + 37.1 * 7, 4, 0, 2 * Math.PI)
    context.fillStyle = "black";
    context.fill();

    clickFlag = true;
}

window.onload = function () {
    loadCanvas();
}


chess.onclick = function (e) {
    var i = (e.offsetX / 37.1) | 0; //得到点击的x坐标
    var j = (e.offsetY / 37.1) | 0; //得到点击的y坐标
    var x = i;
    var y = j;
    oneStep(x, y, true);
}

//这里player true为玩家   false为电脑（下面会写）
function oneStep(x, y, player) {
    isChess[x][y] = 1;
    var color;
    context.beginPath(); //开始画圆
    context.arc(15 + 37.1 * x, 15 + 37.1 * y, 13, 0, 2 * Math.PI) //（x,y,半径，起始点，终止点2*PI即360度）
    context.closePath(); //结束画圆

    if (player) {
        color = "black"; //玩家是黑色
    } else {
        color = "white"; //电脑是红色
    }

    context.fillStyle = color; //设置填充色
    context.fill(); //填充颜色
}


//赢法数组
var wins = [];
for (var i = 0; i < 15; i++) { //定义三维数组
    wins[i] = [];
    for (var j = 0; j < 15; j++) {
        wins[i][j] = [];
    }
}
var count = 0; //（x，y）在的赢法种类
//横线能赢情况
for (var x = 0; x < 11; x++) {
    for (var y = 0; y < 15; y++) {
        for (var z = 0; z < 5; z++) { //z代表向后5个字
            wins[x + z][y][count] = true; //true代表是一种赢法，用count记录下来
        }
        count++; //(x,y)在另一个赢法中
    }
}
//竖线能赢情况
for (var x = 0; x < 15; x++) {
    for (var y = 0; y < 11; y++) {
        for (var z = 0; z < 5; z++) {
            wins[x][y + z][count] = true;
        }
        count++;
    }
}
//正斜线能赢情况
for (var x = 0; x < 11; x++) {
    for (var y = 0; y < 11; y++) {
        for (var z = 0; z < 5; z++) {
            wins[x + z][y + z][count] = true;
        }
        count++;
    }
}
//反斜线能赢情况
for (var x = 0; x < 11; x++) {
    for (var y = 4; y < 15; y++) {
        for (var z = 0; z < 5; z++) {
            wins[x + z][y - z][count] = true;
        }
        count++;
    }
}

//遍历棋盘，是否有棋子，默认为0没有
var isChess = [];
for (var i = 0; i < 15; i++) {
    isChess[i] = [];
    for (var j = 0; j < 15; j++) {
        isChess[i][j] = 0;
    }
}


//人和电脑赢的子占赢法的情况
var manWin = [];
var computerWin = [];
for (var i = 0; i < count; i++) {
    manWin[i] = 0;
    computerWin[i] = 0;
}


//玩家点击下棋
chess.onclick = function (e) {

    if (!clickFlag) {
        return;
    }

    var i = (e.offsetX / 37.1) | 0;
    var j = (e.offsetY / 37.1) | 0;

    var x = i;
    var y = j;
    if (isChess[x][y] == 0) { //是否有棋子，没有下子
        if (type != "4") {
            if (!chessq) {
                layer.msg("轮到对方下棋", {time: 1000});
                return;
            }
            playerw = !playerw;
            oneStep(x, y, playerw); //玩家颜色
            duiyi(toUserId, userId, x, y, playerw);
        } else {
            oneStep(x, y, true);
        }


        for (var i = 0; i < count; i++) { //遍历赢法
            if (wins[x][y][i]) { //（x，y）在赢法i上 该赢法将赢数加一
                manWin[i]++;
            }
            if (manWin[i] == 5) { //为五，赢了
                if (type != "4") {
                    customMessage(toUserId, userId, 3);
                }
                clone();
                layer.alert("你赢了");
            }
        }
        if (type == "4") {
            computerPlayerAction(); //玩家下过棋后该电脑下
        }
    }

}


function computerPlayerAction() {
    var max = 0;
    var u = 0; //电脑棋x坐标
    var v = 0; //电脑棋y坐标

    var manOfValue = []; //玩家赢的权值
    var computerOfValue = []; //电脑赢的权值

    for (var x = 0; x < 15; x++) {
        manOfValue[x] = [];
        computerOfValue[x] = [];
        for (var y = 0; y < 15; y++) {
            manOfValue[x][y] = 0;
            computerOfValue[x][y] = 0;
        }
    }

    for (var x = 0; x < 15; x++) {
        for (var y = 0; y < 15; y++) {
            if (isChess[x][y] == 0) { //查找空白棋
                for (var i = 0; i < count; i++) { //遍历count
                    if (wins[x][y][i]) {
                        if (manWin[i] == 1) {
                            manOfValue[x][y] += 200;
                        } //给予权值
                        else if (manWin[i] == 2) {
                            manOfValue[x][y] += 400;
                        } else if (manWin[i] == 3) {
                            manOfValue[x][y] += 2000;
                        } else if (manWin[i] == 4) {
                            manOfValue[x][y] += 10000;
                        }

                        if (computerWin[i] == 1) {
                            computerOfValue[x][y] += 220;
                        } //电脑相同条件权值要比玩家高，主要还是自己赢
                        else if (computerWin[i] == 2) {
                            computerOfValue[x][y] += 420;
                        } else if (computerWin[i] == 3) {
                            computerOfValue[x][y] += 2200;
                        } else if (computerWin[i] == 4) {
                            computerOfValue[x][y] += 20000;
                        }
                    }
                }


                if (manOfValue[x][y] > max) { //循环判断最大权值
                    max = manOfValue[x][y];
                    u = x;
                    v = y;
                }
                if (computerOfValue[x][y] > max) {
                    max = computerOfValue[x][y];
                    u = x;
                    v = y;
                }


            }
        }
    }
    oneStep(u, v, false); //电脑判断完成，下棋

    for (var i = 0; i < count; i++) { //（x，y）在赢法i上 该赢法将赢数加一
        if (wins[u][v][i]) {
            computerWin[i]++;
        }
        if (computerWin[i] == 5) {
            clone();
            layer.alert("电脑赢了");
        }
    }

}